Kentico Xperience Commerce: The Anonymous User vs. Authenticated User Scenario

   —   

Many times, the question has come up as to how the shopping cart’s behaviour is influenced by the site user; more specifically, whether the site user is authenticated, or not. This short technical article looks at three common scenarios in a typical shopping journey and will highlight the basics around how the shopping cart state is maintained.

Scenario 1

An anonymous (unauthenticated) user

  • An anonymous user adds items to their cart
  • Closes browser
  • Open new browser and browses to site
  • Checks cart contents

Result

Cart maintains items from previous session

How

Cookie

Discussion

An anonymous user’s cart is based on a cookie (set to a unique shopping cart ID), which makes sense as they have not logged in, therefore cannot be linked by user account to a particular cart instance in the database. The anonymous user’s cart is therefore loaded, with their products (if placed in the cart) based on the cookie value associated with their browser.

If we look at the current cookie in the browser, we can see the value as per the below example:

If we look in the database at this time, specifically at the COM_ShoppingCart table, we can see that the ShoppingCartGUID value is set to the same value as the cookie. We can also see that the ShoppingCartUserID field is set to null (as the user is not logged in).

Scenario 2

Authenticated user

  • User Logs in and adds item to their cart
  • Closes browser
  • Opens new browser and browses to site (no cookie from previous browser session)
  • At this point the cart is empty
  • User logs in
  • Checks cart contents

Result

Cart maintains items from previous session

How

User ID database reference

Discussion

As per the above steps, the shopping cart database record is created prior to the user login, at which point the cookie association is as per scenario 1, however once the user logs in, that association becomes based on the authenticated user. Specifically, the ShoppingCartUserID field in the COM_ShoppingCart now holds the value of the CMS_User.UserID of the authenticated user; the cookie association is no longer relevant. If the user logs out, the cart is emptied as it is no longer relying on the cookie. Conversely, if the user logs back in, the cart is reinstated with the cart items for that user based on their UserID.

Scenario 3

Anonymous user populates cart, then logs in with an account which already has a pre-populated cart

  • User Logs in and adds item to their cart
  • User logs out
  • At this stage, due to logout, cart is empty
  • Now acting as an anonymous user, the user adds a new list of items to their cart
  • User logs in

Result

Previously saved items from the authenticated session are now gone. Instead, the items that were added during the later anonymous session now remain in the user’s cart.

Discussion

As per the former scenario 2, with a cart filled with items, when a user logs out the items are removed from the cart. Generally, once the user logs back in the cart items are re-established. However, what happens if the (now anonymous) user then continues adding items to the (now empty) cart.

The question then becomes: What does Kentico Xperience do when the user logs back in? 

  • Does the system ignore the cart items yielded during the anonymous session and give precedence to the existing items which were saved against the user as part of their authenticated session? 
  • Or, does the system consider the saved items from the authenticated session as obsolete, thus giving precedence to the newer items yielded during the anonymous session? 

The answer is the latter — when the user logs back in, the newer items replace the existing items and these now become linked to the authenticated user.

Authenticated user adds items to cart:

User logs out and cart is empty:

Anonymous (unauthenticated) user adds different items to cart:

User logs in and checks cart — latest items are retained:

Discussion

So, what can we do if the above-mentioned processes don’t meet the requirements of our specific solution? Well, the good news is that we can in fact do a lot. Kentico Xperience has always provided developers with the ability to massage in pretty much any custom functionality we want — that’s always been a major strength of the product. Let’s look at the example of scenario 3 from the above examples. What if we want to change the cart behaviour so that saved cart items in the database win over newly acquired items from a more recent anonymous session? We can certainly customise this behaviour as well as many other behaviours, if we so choose. In fact, here is a great guide from the Kentico Xperience documentation which not only describes cart behaviour, but also provides some solid examples on how to customise accordingly:

https://docs.xperience.io/e-commerce-features/customizing-on-line-stores/shopping-cart-related-customizing/retrieving-the-current-shopping-cart

Summary

In summary, Kentico Xperience issues the user’s browser with a cookie based on the unique ID of their shopping cart. This is useful for anonymous users who have no associated user account with the CMS (or who have chosen not to log in), and helps to maintain a seamless shopping experience which allows users to resume their shopping experience between browser sessions.

However, for authenticated users, Kentico Xperience associates their user ID with the shopping cart instance, thus creating a link between secure user entity and the cart. It can sometimes be cause for confusion when a user logs out and suddenly their cart has been emptied — not realising that when they log back in, their cart items will be restored.

Finally, when an authenticated user fills their cart with items and proceeds to log out, their cart is emptied. As they continue on shopping at some later time as an anonymous user and fill their cart with new items, they might decide to log in (perhaps they remembered they already had the items they wanted) — but their prior items have now been replaced with the latest items which they added during their anonymous session. Also, something that might cause a confusing situation.

And remember, the solid API provided by the Kentico team, coupled with unparalleled documentation and support mean that if something doesn’t quite work the way you might need it to, you have the ability to change it and make it your own.

Share this article on   LinkedIn

Jake Kula

Kentico developer since 2008.